package com.example.bottomnavigationdemo;

import androidx.lifecycle.ViewModelProvider;

import android.animation.ObjectAnimator;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FirstFragment extends Fragment {

    private FirstViewModel mViewModel;
    private ImageView imageView;

    public static FirstFragment newInstance() {
        return new FirstFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_first, container, false);
        imageView = view.findViewById(R.id.imageView);
        return view;    //引入图片
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(FirstViewModel.class);   //this:只负责当前页面，切换页面就会初始话   requireActivity():负责所有页面
        imageView.setRotation(mViewModel.rotationPosition);  //每当打开页面时初始化
        // TODO: Use the ViewModel

        //设计动作
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView,"rotation",0,0);   //属性动画，可以对View的所有参数属性都能动画   rotation：动作为旋转
        objectAnimator.setDuration(500);   //动画时间
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!objectAnimator.isRunning()){
                    objectAnimator.setFloatValues(imageView.getRotation(),imageView.getRotation() + 100);   //（当前的角度，角度加100度）
                    mViewModel.rotationPosition += 100;
                    objectAnimator.start();
                }

            }
        });
    }

}